<template>
  <div>
    <a-card>
      <a-form-model :model="form" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-form-model-item label="节日活动">
          <a-switch v-model="form.open" @change="onChange" />
        </a-form-model-item>
        <a-form-model-item label="节日文案">
          <a-textarea v-model="form.value" placeholder="请输入" :auto-size="{ minRows: 5, maxRows: 7 }" />
          <p>tip：换行符请使用@</p>
          <a-button @click="save">保存</a-button>
        </a-form-model-item>
      </a-form-model>
    </a-card>
  </div>
</template>
<script>
import { getGiftCopy, getGiftIfOpen, saveGiftCopy, setGiftIfOpen } from '@/api/box/setting'
export default {
  data() {
    return {
      labelCol: { span: 4 },
      wrapperCol: { span: 10 },
      form: {
        open: false,
        value: null
      }
    }
  },
  mounted() {
    this.initialPage()
  },
  methods: {
    initialPage() {
      getGiftIfOpen().then(res => {
        if (res.code == 0) {
          this.form.open = res.data == 1
        } else {
          this.$message.warning(res.msg)
        }
      })
      getGiftCopy().then(res => {
        if (res.code == 0) {
          this.form.value = res.data
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
    onChange(value) {
      console.log(value)
      let params = value ? 1 : 0
      setGiftIfOpen(params).then(res => {
        if (res.code == 0) {
          this.$message.success(`活动${value ? '启用' : '禁用'}成功`)
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
    save() {
      if (this.form.value) {
        saveGiftCopy(this.form.value).then(res => {
          if (res.code == 0) {
            this.$message.success('文案编辑成功')
          } else {
            this.$message.warning(res.msg)
          }
        })
      } else {
        this.$message.warning('请输入文案')
      }
    }
  }
}
</script>

